﻿using Sustainalytics.AuthOperations;
using Sustainalytics.Utils;
using System;

namespace Sustainalytics.Accounts.Service.Auth
{
    public class AuthProvider : IAuthProvider
    {
        private IAuthOperations _authOperations;

        private AuthProvider()
        {
        }

        public static IAuthProvider GetInstance(IAuthOperations authOperations)
        {
            return new AuthProvider { _authOperations = authOperations };
        }

        public void ChangePassword(string username, string oldPassword, string newPassword)
        {
            try
            {
                _authOperations.ChangePassword(username, oldPassword, newPassword);
            }
            catch (CustomException ex)
            {
                if (ex.InternalCode != null)
                {
                    throw new CustomException(ex.InternalCode.Value, ex.Message);
                }
                throw new ArgumentException(ex.Message);
            }
        }

        public void SetPassword(string username, string password)
        {
            try
            {
                _authOperations.SetPassword(username, password);
            }
            catch (CustomException ex)
            {
                if (ex.InternalCode != null)
                {
                    throw new CustomException(ex.InternalCode.Value, ex.Message);
                }
                throw new ArgumentException(ex.Message);
            }
        }

        public void ForgotPassword(string username)
        {
            try
            {
                //TODO validation should throw specific error not inactive account error code
                var accountIsActive = _authOperations.ValidateAccountStatus(username);

                if (!accountIsActive)
                {
                    //this._authOperations.NotifyUserNotActive(username);
                    throw new CustomException((int)ErrorCodes.ErrorNotifyingUserInactive, "This user is inactive. Please contact your client advisor.");
                }

                _authOperations.ResetPasswordByUsername(username, PasswordEmailMessageBody.ForgotPassword);
            }
            catch (CustomException ex)
            {
                if (ex.InternalCode == (int)ErrorCodes.ErrorNotifyingUserInactive)
                {
                    throw new CustomException((int)ErrorCodes.ErrorNotifyingUserInactive, ex.Message, ex);
                }
                if (ex.InternalCode != null)
                {
                    throw new CustomException(ex.InternalCode.Value, ex.Message);
                }
                throw new ArgumentException(ex.Message);
            }
        }

        public void AcceptLegalAgreement(string username)
        {
            try
            {
                _authOperations.AcceptLegalAgreement(username);
            }
            catch (CustomException ex)
            {
                if (ex.InternalCode != null)
                {
                    throw new CustomException(ex.InternalCode.Value, ex.Message);
                }
                throw new ArgumentException(ex.Message);
            }
        }

        public void AcceptFirstTimeRRUser(string username)
        {
            try
            {
                _authOperations.AcceptFirstTimeRRUser(username);
            }
            catch (CustomException ex)
            {
                if (ex.InternalCode != null)
                {
                    throw new CustomException(ex.InternalCode.Value, ex.Message);
                }
                throw new ArgumentException(ex.Message);
            }
        }

        public void AcceptFirstTimeSPUser(string username)
        {
            try
            {
                _authOperations.AcceptFirstTimeSPUser(username);
            }
            catch (CustomException ex)
            {
                if (ex.InternalCode != null)
                {
                    throw new CustomException(ex.InternalCode.Value, ex.Message);
                }
                throw new ArgumentException(ex.Message);
            }
        }
    }
}
